package com.mango.iot.gateway.http.controller;

import com.alibaba.fastjson.JSONObject;
import com.mango.iot.gateway.http.consts.AppBizException;
import com.mango.iot.gateway.http.consts.JsonResult;
import com.mango.iot.gateway.http.controller.req.AuthReq;
import com.mango.iot.gateway.http.controller.resp.AuthResp;
import com.mango.iot.gateway.http.srv.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 登陆派发token接口
 *
 * @author liangfeihu
 * @since 2021/8/24 11:51
 */
@Slf4j
@RestController
public class AuthController {

    @Autowired
    private AuthService authService;

    @PostMapping("/auth")
    public JsonResult auth(@Valid @RequestBody AuthReq authReq) throws AppBizException {
        log.info("[auth] params={}", JSONObject.toJSONString(authReq));
        String token = authService.handleDeviceAuth(authReq);
        return JsonResult.success("auth success", new AuthResp(token));
    }

}
